<?php

    $record = new record_class();

    // Результаты записи
    
    class entry_class
    {
        public $guid;
        public $date;
        public $time;
        public $place;
        public $speciality;
        public $doctorfio;
        public $division;
        public $surgery;
    }
    
    class direction_patient_data_class
    {
        public $fio;
        public $document;
        public $birthday;
		public $sex;
        public $policy;
        public $employment;
    }
    
    class direction_class
    {
        public $guid;
        public $number;
        public $date;
        public $director;
        public $patient;
        public $entries = Array();
        
        public function __construct()
        {
            $this->patient = new direction_patient_data_class();
        }
    }

    class record_class
    {

        public $directions = Array();
        public $address = null;
        
        public function __construct()
        {
            // $this->getdata();
        }
        
        public function items()
        {
            return $this->directions;
        }

        public function getdata($month, $year)
        {
            global $mikron;
            $remotedirection = isset($_REQUEST['okremotedirection'])?$_REQUEST['okremotedirection']:null;
            $qr = $mikron->Queries->Query('T_PERSON', null, new Criterion('cabinet', $mikron->Users->CurrentUser()->id()), null, null, null, null, Array('uniqueid'));
            $persons = Array();
            while($person = $qr->fetch())
            {
                $persons[] = $person->uniqueid->value;
            }
            if(count($persons) == 0)
            {
                return true;
            }            
            $xml = CreateGetRemoteDirectionsXML($remotedirection, 'part', $month, $year, $persons);
            $buf = CallSoap('getremotedirections', $xml);
            $dom1 = new DomDocument();
            $dom1->loadXML($buf);
            $directions_elm = $dom1->getElementsByTagName('remotedirections');

            foreach($directions_elm as $directionslist)
            {
                $this->address = $directionslist->getAttribute('address');
                $remotedirections = $directionslist->getElementsByTagName('remotedirection');
                foreach($remotedirections as $remotedirection)
                {
                    $direction = new direction_class();
                    $direction->guid = $remotedirection->getAttribute("guid");
                    $direction->date = $remotedirection->getAttribute("date");
                    $direction->number = $remotedirection->getAttribute("number");
                    $direction->director = $remotedirection->getAttribute("director");
                    $patients = $remotedirection->getElementsByTagName("patient");
                    foreach($patients as $patient)
                    {
                        $direction->patient->fio = $patient->getAttribute("fio");
                        $direction->patient->document = $patient->getAttribute("document");
                        $direction->patient->birthday = date('d.m.Y', strtotime($patient->getAttribute("birthday")));
					    $direction->patient->sex = $patient->getAttribute("sex");
                        $direction->patient->policy = $patient->getAttribute("policy");
                        $direction->patient->employment = $patient->getAttribute("employment");
                    }
                    
                    $entrieslist = $remotedirection->getElementsByTagName("entries");
                    foreach($entrieslist as $entries)
                    {
                        $entrylist = $remotedirection->getElementsByTagName('entry');
                        foreach($entrylist as $entry_element)
                        {
                            $entry = new entry_class();
                            $entry->guid = $entry_element->getAttribute("guid");
                            $entry->date = $entry_element->getAttribute("date");
                            $entry->time = $entry_element->getAttribute("time");
                            $entry->surgery = $entry_element->getAttribute("surgery");
                            $entry->place = $entry_element->getAttribute("place");
                            $entry->speciality = $entry_element->getAttribute("speciality");
                            $entry->doctorfio = $entry_element->getAttribute("doctorfio");
                            $entry->division = $entry_element->getAttribute("division");
                            $direction->entries[] = $entry;                            
                        }
                    }
                    
                    $this->directions[] = $direction;
                }
            }

        }
    }

?>